package com.dbh.company.chaos;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 *
 * @author dongbanghui
 * @create 2023/6/20
 */
public class FullGC {

    public Object run(Map<String, Object> params) throws InterruptedException {
        Logger logger = LoggerFactory.getLogger(FullGC.class);
        logger.info("注入fullGC攻击开始...");
        // 循环创建对象并返回
        long startTime = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime < 1000 * 30) {
            for (int i = 0; i < 10000; i++) {
                fullGC();
            }
        }
        Thread.sleep(1000 * 3);
        long startTime2 = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime2 < 1000 * 30) {
            for (int i = 0; i < 10000; i++) {
                fullGC();
            }
        }
        logger.info("注入fullGC攻击结束...");
        return null;
    }

    public byte[] fullGC() {
        byte[] bytes = new byte[1024 * 1024 * 512];
        return bytes;
    }

}
